
import FileResult from '../types/file.ts'
import UploadOptions from '../types/uploadOption.ts';
const uploadFile: (file: File) => Promise<string | ArrayBuffer | null> = (file: File) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onloadend = function () {
            resolve(reader.result);
            return
        };
        // 根据文件类型进行不同处理
        if (file.type.startsWith('image/')) {
            // 处理图片文件
            reader.readAsDataURL(file);
        } else if (file.type.startsWith('text/') || file.type.endsWith('/json') || file.type === '') {
            reader.readAsText(file);
        } else {
            reject('不支持的文件类型')
        }
    })
}
const upload: (uploadOptions) => Promise<FileResult> = (options: UploadOptions = { accept: 'image/*', multiple: false, uploader: uploadFile }) => {
    return new Promise((resolve, reject) => {
        const fileInput = document.createElement('input');
        fileInput.type = 'file';
        fileInput.accept = options.accept || '';
        fileInput.multiple = options.multiple || false;
        // 隐藏 fileInput 元素
        fileInput.style.display = 'none';
        fileInput.addEventListener('change', function (e) {
            // 假设event是某个文件输入元素的change事件
            const inputElement = e.target as HTMLInputElement;
            const file = inputElement.files && inputElement.files[0];
            // 检查是否选择了文件
            if (!file) {
                reject("请选择一个文件");
                return;
            }
            // 检查文件大小
            const MAX_FILE_SIZE = 5 * 1024 * 1024; // 最大文件大小为 5MB
            if (file?.size > MAX_FILE_SIZE) {
                reject(`文件大小超过限制 (${MAX_FILE_SIZE / (1024 * 1024)} MB)`)
                return;
            }
            options.uploader = options.uploader || uploadFile;
            if (!options.uploader) {
                reject("未设置上传函数")
                return;
            }
            options.uploader(file).then((result) => {
                document.body.removeChild(fileInput);
                resolve({ type: file?.type, name: file?.name, data: result });
            }).catch(
                err => {
                    document.body.removeChild(fileInput);
                    reject(err)
                }
            );
        });
        document.body.appendChild(fileInput);
        // 触发文件选择框
        fileInput.click();
    })
}

export { upload, uploadFile }